<?php

namespace app\admin\system\logic;

use app\admin\system\service\AdminService;
use app\common\logic\BaseLogic;
use app\model\SysMenu;
use app\model\SysRoleMenu;

class MenuLogic extends BaseLogic
{
    public function __construct()
    {
        $this->model = new SysMenu();
    }

    public function listRouter($uid)
    {
        $isSuperAdmin = AdminService::isSuperAdmin($uid);

        if ($isSuperAdmin) {
            $data = SysMenu::alias('a')
                ->where('a.menuType', 'in', [1, 2])
                ->order("a.sorted")
                ->field([
                    "a.menuId",
                    "a.menuName",
                    "a.parentId",
                    "a.menuType",
                    "a.path",
                    "a.name",
                    "a.component",
                    "a.icon",
                    "a.isHide",
                    "a.isLink",
                    "a.isKeepAlive",
                    "a.isFull",
                    "a.isAffix",
                    "a.redirect",
                ])
                ->select();
        } else {
            $role_ids = AdminService::getRoleIdsByUid($uid);
            $data = SysMenu::alias('a')
                ->join(SysRoleMenu::getTable() . ' b', 'a.menuId=b.menu_id')
                ->where('b.role_id', 'in', $role_ids)
                ->where('a.menuType', 'in', [1, 2])
                ->order("a.sorted")
                ->field([
                    "a.menuId",
                    "a.menuName",
                    "a.parentId",
                    "a.menuType",
                    "a.path",
                    "a.name",
                    "a.component",
                    "a.icon",
                    "a.isHide",
                    "a.isLink",
                    "a.isKeepAlive",
                    "a.isFull",
                    "a.isAffix",
                    "a.redirect",
                ])
                ->select();
        }
        return $data;
    }

}